﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SpellCorrector
{
    internal class BkTreeNode:Node
    {
        public BkTreeNode (Node n)
        {
            this.NumberofTimesSeenBefore = n.NumberofTimesSeenBefore;
            this.Word = n.Word;
        }
        private Dictionary<int, BkTreeNode> _Children=new Dictionary<int,BkTreeNode>();
        
        public void AddChildNode(BkTreeNode childNode)

        {
            int distance = CalculateLevenshteinDistance(this.Word,childNode.Word);
            BkTreeNode nodeAtDistance;
            _Children.TryGetValue(distance,out nodeAtDistance);
            if (nodeAtDistance == null)
            {
                _Children.Add(distance, childNode);
            }
            else
            {
                nodeAtDistance.AddChildNode(childNode);
            }

        }
        public BkTreeNode GetNodeAtDistance(int distance)
        {
            BkTreeNode nodeAtDistance;
            _Children.TryGetValue(distance, out nodeAtDistance);
            return nodeAtDistance;
        }
        public List<BkTreeNode> GetNodesAtDistanceRange(int upperBound, int lowerBound)
        {
            List<BkTreeNode> result = new List<BkTreeNode>();
            var v = from int n in _Children.Keys where n <= upperBound && n >= lowerBound select n;
            foreach (int n in v.ToArray())
            {
                BkTreeNode nodeAtDistance;
                _Children.TryGetValue(n, out nodeAtDistance);
                result.Add(nodeAtDistance);
            }
        
            return result;
        }
    }
}
